package com.devin.service.Impl;

import com.devin.dao.MultiMapper;
import com.devin.dao.RoleMapper;
import com.devin.dao.UserMapper;
import com.devin.pojo.Role;
import com.devin.pojo.UserInfo;
import com.devin.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MultiMapper multiMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public List<UserInfo> getUsers(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return userMapper.getUsers();
    }
    @Override
    public void addUser(UserInfo user,Integer[] roleIds){
        /*加密，密码*/
        String encode = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encode);

        /*添加用户*/
        userMapper.addUser(user);
        /*添加用户对应的角色*/
        multiMapper.addUserAndRoles(user.getId(),roleIds);
    }

    /*通过userId查询用户的具体信息，包括角色信息*/
    @Override
    public UserInfo getUserById(Integer userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public void delRoleByUserId(Integer userId, Integer roleId) {
        userMapper.delRoleByUserId(userId,roleId);
    }

    @Override
    public List<Role> getRoles2Json(Integer userId) {
        return roleMapper.getRoles2Json(userId);
    }

    @Override
    public void addRoles(Integer userId, Integer[] roleIds) {
        multiMapper.addUserAndRoles(userId,roleIds);
    }

    @Override
    public void delUser(Integer userId) {
        userMapper.delUserAndRoles(userId);
        userMapper.delUser(userId);
    }

    @Override
    public void delUsersChecked(Integer[] userIds) {
        multiMapper.delUsersAndRoles(userIds);
        multiMapper.delUsers(userIds);
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("==============================");
        UserInfo user = userMapper.getUser(s);
        User user1 = new User(user.getUsername(),user.getPassword(),true,/*user.getStatus()==1?true:false,*/true,true,true,getAuthorities(user.getRoles()));
        return user1;
    }

    public List<SimpleGrantedAuthority> getAuthorities(List<Role> roles){
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for(Role role:roles){
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return authorities;
    }

}

